#region Using Statements
using System;
using System.IO;
using System.Xml;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Runtime.Remoting;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion

namespace SkyBurner
{
    // PostScreenEffectBase class
    //
    #region PostScreenEffectBase class
    public class PostScreenEffectBase : BaseObject
    {
        protected float m_fEffectLifeTime;
        protected Effect m_customEffect;

        public bool Active
        {
            get { return (m_fEffectLifeTime == -1.0f) || (m_fEffectLifeTime > 0.0f); }
        }

        public PostScreenEffectBase(BaseGame pGame) : base(pGame)
        {
            m_customEffect = null;
        }

        public virtual void Begin(GameTime gameTime, SpriteBatch pSpriteBatch)
        {
            pSpriteBatch.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch pSpriteBatch, Rectangle pScreenRect, Texture2D pScreenTexture)
        {
            m_customEffect.Begin();
            foreach (EffectPass pass in m_customEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                pSpriteBatch.Draw(pScreenTexture, pScreenRect, Color.White);
                pass.End();
            }
            m_customEffect.End();

            if (m_fEffectLifeTime > 0.0f)
            {
                m_fEffectLifeTime -= gameTime.ElapsedGameTime.Milliseconds;

                if (m_fEffectLifeTime < 0.0f)
                    m_fEffectLifeTime = 0.0f;
            }                
        }

        public virtual void End(SpriteBatch pSpriteBatch)
        {
            pSpriteBatch.End();
        }
    }
    #endregion

    // PostScreenEffectBlur class
    //
    #region PostScreenEffectBlur class
    public class PostScreenEffectBlur : PostScreenEffectBase
    {
        private float m_fBlurFactor;

        public PostScreenEffectBlur(BaseGame pGame, float pBlurFactor) 
            : base(pGame)
        {
            m_fBlurFactor = pBlurFactor;
            m_fEffectLifeTime = -1.0f; // neverending effect
            m_customEffect = m_Game.ContentManager.Load<Effect>("Data\\Shaders\\ScreenBlur");
        }

        public override void Draw(GameTime gameTime, SpriteBatch pSpriteBatch, Rectangle pScreenRect, Texture2D pScreenTexture)
        {
            m_customEffect.Parameters["BlurFactor"].SetValue(m_fBlurFactor);

            base.Draw(gameTime, pSpriteBatch, pScreenRect, pScreenTexture);
        }
    }
    #endregion

    // PostScreenEffectDistorted class
    //
    #region PostScreenEffectDistorted class
    public class PostScreenEffectDistorted : PostScreenEffectBase
    {
        private float m_fTick;
        private float m_fTickIncrement;

        public PostScreenEffectDistorted(BaseGame pGame, float fEffectLifeTime, float fTickIncrement)
            : base(pGame)
        {
            m_fTick = 0.0f;
            m_fTickIncrement = fTickIncrement;
            m_fEffectLifeTime = fEffectLifeTime;
            m_customEffect = m_Game.ContentManager.Load<Effect>("Data\\Shaders\\ScreenDistorsion");
        }

        public override void Draw(GameTime gameTime, SpriteBatch pSpriteBatch, Rectangle pScreenRect, Texture2D pScreenTexture)
        {
            m_customEffect.Parameters["tick"].SetValue(m_fTick);
            m_fTick += m_fTickIncrement;

            base.Draw(gameTime, pSpriteBatch, pScreenRect, pScreenTexture);
        }

    }
    #endregion

    // PostScreenEffectMotionBlur class
    //
    #region PostScreenEffectMotionBlur class
    public class PostScreenEffectMotionBlur : PostScreenEffectBase
    {
        private bool m_bFirstFrame;
        private float m_fAplhaFactor;

        public PostScreenEffectMotionBlur(BaseGame pGame, float pAlphaFactor)
            : base(pGame)
        {            
            m_bFirstFrame = true;
            m_fEffectLifeTime = -1.0f;
            m_fAplhaFactor = pAlphaFactor;
            m_customEffect = m_Game.ContentManager.Load<Effect>("Data\\Shaders\\ScreenMotionBlur");            
        }

        public override void Begin(GameTime gameTime, SpriteBatch pSpriteBatch)
        {
            pSpriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
        }

        public override void Draw(GameTime gameTime, SpriteBatch pSpriteBatch, Rectangle pScreenRect, Texture2D pScreenTexture)
        {
            if (m_bFirstFrame)
            {               
                m_customEffect.Parameters["alpha"].SetValue(1.0f - m_fAplhaFactor);
                m_bFirstFrame = false;
            }
            else
                m_customEffect.Parameters["alpha"].SetValue(m_fAplhaFactor);

            base.Draw(gameTime, pSpriteBatch, pScreenRect, pScreenTexture);
        }
    }
    #endregion
}
